package com.hollycrm.springframework.dbaccesslog.hibernate;

import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import com.hollycrm.springframework.dbaccesslog.DBAccessLog;
import com.hollycrm.springframework.dbaccesslog.DBAccessManager;

public class HibernateDBAccessManager extends HibernateDaoSupport implements DBAccessManager {

    /**
     * initialLog
     *
     * @param serviceName String
     * @param sqlText String
     * @return DBAccessLog
     */
    public DBAccessLog initialLog(String serviceName, String sqlText) {
        throw new UnsupportedOperationException("not implement this method, use proxy.");
    }

    /**
     * saveLog
     *
     * @param log DBAccessLog
     * @param isComplete boolean
     */
    public void saveLog(DBAccessLog log, boolean isComplete) {
        if (log != null && log.getSqlText() != null) {
            byte[] tBytes = log.getSqlText().getBytes();
            int kiloLength = 1000;
            if (tBytes.length > kiloLength)
                log.setSqlText(new String(log.getSqlText().getBytes(), 0, kiloLength));
            getHibernateTemplate().save(log);
        }
    }
}
